import java.util.Scanner;

/**
 * @version 1.0
 * @program: JavaStage4Project
 * @description: 居民位置
 * @author: Kishan Liu
 * @create: 2021-12-16 15:36
 **/
public class ResidentsPosition {
    static int n;
    static int t;
    static int k;
    static int x1;
    static int y1;
    static int x2;
    static int y2;
    static int pass=0;
    static int sojourn=0;

    public static void printIntroduction() {
        System.out.println("************************************欢迎使用本程序************************************");
        System.out.println("本程序可根据预设的判断标准检测录入的市民位置坐标，检测出经过高危区域的人数和曾在高危区域逗留的人数。");
        System.out.println("本程序需要预设居民数量、在高危区域逗留的判断标准(居民位置坐标在高危区域的个数)、每个居民的位置坐标个数、高危区域的范围(左下角和右上角的坐标)");

    }

    /**
     *设置基本参数
     */
    public static void setInformation() {
        for (; ; ) {
            System.out.print("请输入居民数量:");
            Scanner scanner = new Scanner(System.in);
            if (scanner.hasNextInt()) {
                if ((n = scanner.nextInt())>0) {
                    System.out.println("输入成功！居民数量为:" + n);
                    break;
                }
                else System.out.println("请输入大于0的整数！");
            } else {
                System.out.println("请输入整数！");
            }
        }

        for (; ; ) {
            System.out.print("在高危区域逗留的判断标准(居民位置坐标在高危区域的个数):");
            Scanner scanner = new Scanner(System.in);
            if (scanner.hasNextInt()) {
                if ((k = scanner.nextInt())>0){
                    System.out.println("输入成功！判断标准为"+k+"个。");
                    break;
                }
                else System.out.println("请输入大于0的整数！");
            } else {
                System.out.println("请输入整数！");
            }
        }

        for (; ; ) {
            System.out.print("每个居民的位置坐标个数:");
            Scanner scanner = new Scanner(System.in);
            if (scanner.hasNextInt()) {
                if ((t = scanner.nextInt())>=k){
                    System.out.println("输入成功！每个居民的位置坐标个数为"+k+"个。");
                    break;
                }
                else System.out.println("请输入不小于判断个数的整数！");
            } else {
                System.out.println("请输入整数！");
            }
        }


        System.out.println("高危区域的范围(左下角和右上角的坐标):");
        for (boolean flag1 = false; ; ) {
            System.out.print("左下的坐标(用空格隔开):");
            Scanner scanner = new Scanner(System.in);
            for (int i = 0; i < 2; i++) {
                if (i == 0 && scanner.hasNextInt()) {
                    x1 = scanner.nextInt();
                } else if (i == 1 && scanner.hasNextInt()) {
                    y1 = scanner.nextInt();
                    flag1 = true;
                } else {
                    System.out.println("请输入整数！");
                    break;
                }
            }
            if (flag1) {
                System.out.println("输入成功！左下的坐标为（"+x1+","+y1+")。");
                break;
            }
        }
        for (boolean flag2 = false; ; ) {
            System.out.print("右上的坐标(用空格隔开):");
            Scanner scanner = new Scanner(System.in);
            for (int i = 0; i < 2; i++) {
                if (i == 0 && scanner.hasNextInt()) {
                    x2 = scanner.nextInt();
                } else if (i == 1 && scanner.hasNextInt()) {
                    y2 = scanner.nextInt();
                    flag2 = true;
                } else {
                    System.out.println("请输入整数！");
                    break;
                }
            }
            if (flag2) {
                System.out.println("输入成功！右上的坐标为（"+x2+","+y2+")。");
                break;
            }
        }

    }


    /**
     * 录入一个市民位置的坐标
     * @param numCitizen 表示输入的第几个市民的位置坐标
     */
    public static int[][] setPoint(int numCitizen){
        System.out.println("请输入第"+numCitizen+"个居民的位置坐标(数据之间用空格隔开):");
        int[][] point=new int[t][2];
        for(boolean flag=false;;){
            Scanner scanner = new Scanner(System.in);
            for (int i = 0; i < t; i++) {
                for (int j = 0; j < 2; j++) {
                    if (j==0 && scanner.hasNextInt()){
                        point[i][j] = scanner.nextInt();
                    }else if (j==1 && scanner.hasNextInt()){
                        point[i][j] = scanner.nextInt();
                        flag=true;
                    }else {
                        System.out.println("请输入整数！");
                        break;
                }
                }
            }
            if (flag) break;
        }
        System.out.print("输入成功！该居民的位置坐标分别为：");
        for (int i = 0; i < t; i++) {
            System.out.print("("+point[i][0]+","+point[i][1]+")  ");
        }
        System.out.println("\n");
        return point;
    }

    /**
     * 检索一个居民有几个高危位置
     * @return 该居民高危位置的个数
     */
    public static int judgeNumber(int[][]point) {
        int number = 0;
        for (int[] ints : point) {
            if (ints[0] >= x1 && ints[0] <= x2 && ints[1] >= y1 && ints[1] <= y2) {
                number++;
            }
        }
        return number;
    }

    public static int judgePass(int number) {
        if (number > 0)return 1;
        else return 0;
    }

    public static int judgeSojourn(int number) {
        if (number >= k)return 1;
        else return 0;
    }

    public static void mainMenu(){
        printIntroduction();
        setInformation();
        for (int i = 1; i <= n; i++) {
            int num = judgeNumber(setPoint(i));
            pass+=judgePass(num);
            sojourn+=judgeSojourn(num);
        }
        System.out.print("一共有"+pass+"人经过高危区域。\n     "+sojourn+"人曾在高危区域逗留。");
    }
}

